﻿
// Condition: 9. Write a method that return the maximal element in a portion of array of integers starting at given index.
            

using System;



class MaxElementInPortion
{
    // Sort the array in descending order
    static int MaxSum(int[] array1, int index1)
    {
        for (int i = index1; i < array1.Length - 1; i++)
        {
            for (int j = i + 1; j < array1.Length; j++)
            {
                if (array1[i] > array1[j])
                {
                    array1[i] = array1[i] + array1[j];
                    array1[j] = array1[i] - array1[j];
                    array1[i] = array1[i] - array1[j];
                }
            }
        }
        // return the maximum element in the sorted array
        return array1[array1.Length - 1];
    }

    static void Main(string[] args)
    {
        // read array from the console
        Console.Write("Enter number of elements in array: ");
        int num = int.Parse(Console.ReadLine());
        int[] array = new int[num];
        for (int k = 0; k < num; k++)
        {
            Console.Write("Element[{0}] : ",k);
            array[k] = int.Parse(Console.ReadLine());
        }
        // print the array
        Console.WriteLine("Initial array: ");
        for (int k = 0; k < array.Length; k++)
        {
            Console.Write(array[k]+" ");
        }
        Console.WriteLine();

        // Enter the start element for subarray 
        Console.Write("Enter index between 0 and {0}, to return the max element in subarray: ",(array.Length-1));
        int index = int.Parse(Console.ReadLine());

        Console.Write("The subarray is: { ");
        for (int k = index; k < array.Length; k++)
        {
            Console.Write(array[k]+" ");
        }
        Console.Write("}");
        Console.WriteLine();
        // print the maximum element in subarray
        Console.WriteLine("The maximum element in subarray is: {0}",MaxSum(array, index));
    }
}
